package ciyuan.Entity.ExplosionTrap;

import net.minecraft.entity.*;
import net.minecraft.entity.ai.attributes.AttributeModifierMap;
import net.minecraft.entity.ai.attributes.Attributes;
import net.minecraft.entity.monster.MonsterEntity;
import net.minecraft.network.IPacket;
import net.minecraft.potion.EffectInstance;
import net.minecraft.potion.Effects;
import net.minecraft.util.DamageSource;
import net.minecraft.util.math.AxisAlignedBB;
import net.minecraft.world.Explosion;
import net.minecraft.world.World;
import net.minecraftforge.fml.network.NetworkHooks;

import java.util.List;

public class ExplosionTrapEntity extends MobEntity {

    private static final EntityPredicate PARTNER_TARGETING = (new EntityPredicate()).range(1.0D);

    public ExplosionTrapEntity(EntityType<ExplosionTrapEntity> entityEntityType, World world) {
        super(entityEntityType,world);
    }

    private long time=System.currentTimeMillis();

    @Override
    public void tick() {
        super.tick();
        if(System.currentTimeMillis()-time>10000) {
            this.level.explode(null, this.getX(), this.getY()+0.625, this.getZ(), 1.0F, Explosion.Mode.NONE);
            List<LivingEntity> livingEntityList = this.level.getNearbyEntities(LivingEntity.class,PARTNER_TARGETING,this,
                    new AxisAlignedBB(this.getX()+2,this.getY()+2,this.getZ()+2,this.getX()-2,this.getY()-2,this.getZ()-2));
            for (LivingEntity livingEntity:livingEntityList) {
                livingEntity.hurt(DamageSource.MAGIC,10);
                livingEntity.addEffect(new EffectInstance(Effects.MOVEMENT_SLOWDOWN,100,3));
            }
            this.remove();
        }
    }

    @Override
    public void push(double p_70024_1_, double p_70024_3_, double p_70024_5_) {
        this.level.explode(null, this.getX(), this.getY()+0.625, this.getZ(), 1.0F, Explosion.Mode.NONE);
        List<LivingEntity> livingEntityList = this.level.getNearbyEntities(LivingEntity.class,PARTNER_TARGETING,this,
                new AxisAlignedBB(this.getX()+2,this.getY()+2,this.getZ()+2,this.getX()-2,this.getY()-2,this.getZ()-2));
        for (LivingEntity livingEntity:livingEntityList) {
            livingEntity.hurt(DamageSource.MAGIC,10);
            livingEntity.addEffect(new EffectInstance(Effects.MOVEMENT_SLOWDOWN,100,3));
        }
        this.remove();
    }

    @Override
    public boolean isPickable() {
        return false;
    }

    @Override
    public IPacket<?> getAddEntityPacket() {
        return NetworkHooks.getEntitySpawningPacket(this);
    }

    public static AttributeModifierMap.MutableAttribute createAttributes() {
        return MonsterEntity.createMonsterAttributes().add(Attributes.MAX_HEALTH,1).add(Attributes.MOVEMENT_SPEED, (double)0.0F);
    }

}
